package com.dawn.trademanage.serviceimpl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dawn.trademanage.bean.TradeInfoBean;
import com.dawn.trademanage.dao.TradeManageDao;
import com.dawn.trademanage.service.TradeManageService;
import com.dawn.utils.exceptions.DaoAccessException;

@Service
public class TradeManageServiceImpl implements TradeManageService {
    
    private static Logger logger = Logger.getLogger(TradeManageServiceImpl.class);

    @Autowired
    private TradeManageDao tradeManageDao;

    @Override
    public boolean addTrade(TradeInfoBean tradeInfo) {
        try {
            tradeManageDao.addTrade(tradeInfo);
        } catch (Exception e) {
            logger.error("Data access error.");
            e.printStackTrace();
            throw new DaoAccessException();
        }
        return true;
    }

    @Override
    public List<TradeInfoBean> showTradeList() {
        List<TradeInfoBean> list = null;
        try {
            list = tradeManageDao.showTradeList();
        } catch (Exception e) {
            logger.error("Data access error.");
            e.printStackTrace();
            throw new DaoAccessException();
        }
        return list;
    }

    @Override
    public TradeInfoBean findTradeById(int tradeId) {
        List<TradeInfoBean> list = null;
        try {
            list = tradeManageDao.findTradeById(tradeId);
            if(!list.isEmpty()){
                TradeInfoBean tradeInfo = list.get(0);
                return tradeInfo;
            }
        } catch (Exception e) {
            logger.error("Data access error.");
            e.printStackTrace();
            throw new DaoAccessException();
        }
        return null;
    }
}
